﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1000
{
    [Plug(ID = "cMES1000",
          Name = "供应商资料",
          Description = "供应商资料维护",
          Parent = "0,基础数据")]
    public partial class cMES1000 : StdView
    {
        public cMES1000()
        {
            InitializeComponent();
            datalist.CellMouseDoubleClick += Datalist_CellMouseDoubleClick;
        }
        private void cMES1000_Load(object sender, EventArgs e)
        {
            //this.LoadVendorList();
        }

        private void LoadVendorList()
        {
            ExecutionResult<dynamic> res = Services.ServiceMethod.GetVendorList();

            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);

                return;
            }

            datalist.DataSource = res.Data;

            datalist.ClearSelection();
        }

        private void Datalist_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                Edit edit = new Edit("EDIT", new object[]
                {
                    datalist.SelectedRows[0].Cells[0].Value,
                    datalist.SelectedRows[0].Cells[1].Value,
                    datalist.SelectedRows[0].Cells[2].Value,
                    datalist.SelectedRows[0].Cells[3].Value,
                    datalist.SelectedRows[0].Cells[4].Value,
                    datalist.SelectedRows[0].Cells[5].Value
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadVendorList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            ExecutionResult<dynamic> res = Services.ServiceMethod.GetVendorByID(txtSearch.Text);

            if (!res.Status)
            {
                ShowMessage(res.Message, MsgType.NG);
            }
            else
            {
                if (res.Data.Count > 0)
                {
                    datalist.DataSource = res.Data;
                }
                else
                {
                    datalist.Rows.Clear();
                    ShowMessage($"没有查到：{txtSearch.Text}", MsgType.NG);
                }

                datalist.ClearSelection();
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            Edit edit = default(Edit);

            if (((ToolStripButton)sender).Tag.Equals("ADD"))
            {
                edit = new Edit("ADD");

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据添加成功！", MsgType.OK);

                    this.LoadVendorList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据添加失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("EDIT"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个供应商信息进行编辑.", MsgType.NG);
                    return;
                }
                edit = new Edit("EDIT", new object[] 
                {
                    datalist.SelectedRows[0].Cells[0].Value,
                    datalist.SelectedRows[0].Cells[1].Value,
                    datalist.SelectedRows[0].Cells[2].Value,
                    datalist.SelectedRows[0].Cells[3].Value,
                    datalist.SelectedRows[0].Cells[4].Value,
                    datalist.SelectedRows[0].Cells[5].Value
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadVendorList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("DELETE"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个需要删除的供应商信息.", MsgType.NG);
                    return;
                }

                DialogResult dialog = MessageBox.Show($"确定要删除{datalist.SelectedRows[0].Cells[0].Value}吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

                if (dialog == DialogResult.Yes)
                {
                    ExecutionResult result = Services.ServiceMethod.DeleteVendor(datalist.SelectedRows[0].Cells[0].Value.ToString());

                    if (result.Status && result.ExecuteCount > 0)
                    {
                        this.ShowMessage($"数据{datalist.SelectedRows[0].Cells[0].Value}删除成功！", MsgType.OK);

                        this.LoadVendorList();
                    }
                }
            }
        }
    }
}
